package com.atguigu.chapter07;

import com.atguigu.chapter05.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.*;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/3/6 9:12
 */
public class Flink28_State_KeyedDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> sensorDS = env
                .socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        // 切分
                        String[] line = value.split(",");
                        return new WaterSensor(line[0], Long.valueOf(line[1]), Integer.valueOf(line[2]));

                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()
                                .withTimestampAssigner((value, ts) -> value.getTs() * 1000L)
                );

        KeyedStream<WaterSensor, String> sensorKS = sensorDS.keyBy(sensor -> sensor.getId());

        SingleOutputStreamOperator<Long> resultDS = sensorKS
                .process(
                        new KeyedProcessFunction<String, WaterSensor, Long>() {

                            // TODO 1.定义状态
                            ValueState<String> valueState;
                            ListState<Integer> listState;
                            MapState<Long, Double> mapState;
                            ReducingState<Integer> reducingState;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                // TODO 2.在open方法里获取状态
                                valueState = getRuntimeContext().getState(new ValueStateDescriptor<String>("valueState", Types.STRING));
                                listState = getRuntimeContext().getListState(new ListStateDescriptor<Integer>("listState", Types.INT));
                                mapState = getRuntimeContext().getMapState(new MapStateDescriptor<Long, Double>("mapState", Types.LONG, Types.DOUBLE));
                                reducingState = getRuntimeContext().getReducingState(new ReducingStateDescriptor<Integer>("reducingState", Integer::sum, Types.INT));
                            }

                            @Override
                            public void processElement(WaterSensor value, Context ctx, Collector<Long> out) throws Exception {
                                // TODO 3.使用状态
//                                valueState.value();  // 取出 值状态的 值
////                                valueState.update();    // 更新 值状态 的值
//                                valueState.clear(); // 清空的是 当前key 的状态
//
//                                listState.add();   // 添加单个值
//                                listState.addAll(); // 添加整个 List
//                                listState.update(); // 更新整个 List
//                                listState.get();    // 获取 list的值
//                                listState.clear();  // 清空 当前key 的状态
//
//
//                                mapState.clear(); // 清空 当前key 的状态，其他使用跟Map一样
//
//
//                                reducingState.add();    // 添加单个值，添加之后会执行聚合逻辑
//                                reducingState.get();    // 获取 状态值
//                                reducingState.clear();  // 清空当前key 的状态
                            }


                        }
                );

        resultDS.print("result");

        env.execute();
    }
}

/*


 */